package com.shareyi.study.groovyscript.impl;

import com.shareyi.study.groovyscript.GroovyExecutor;

import javax.script.*;
import java.util.Map;

/**
 * Created by david on 2017/3/11.
 */
public class GroovyScriptExecutor implements GroovyExecutor {

    static ScriptEngineManager factory = null;
    static ScriptEngine engine = null;

    static {
         factory = new ScriptEngineManager();
         engine = factory.getEngineByName("groovy");
    }


    public Object executeScript(String script, Map<String, Object> param) {

        Bindings bindings= engine.createBindings();
        bindings.putAll(param);
        try {
            return engine.eval(script,bindings);
        } catch (ScriptException e) {
            e.printStackTrace();
        }
        return null;
    }
}
